package com.plian.system.controller.im;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.*;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.im.FundProjectInfo;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.im.IFundProjectInfoService;
import com.plian.system.vo.im.FundProjectInfoVO;
import com.plian.system.wrapper.im.FundProjectInfoWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 控制器
 *
 * @author
 * @since 2019-09-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/fundprojectinfo")
@Api(value = "FundProjectInfoController", tags = "市属国企基金项目情况 控制器")
public class FundProjectInfoController extends BaseController {

	private IFundProjectInfoService fundProjectInfoService;

	private FundProjectInfoWrapper fundProjectInfoWrapper;

	/**
	 * 详情
	 */
	@PostMapping("/detail")
	@ApiOperation(value = "详情")
	public R<FundProjectInfoVO> detail(@RequestBody FundProjectInfo fundProjectInfo) {
		return R.data(fundProjectInfoService.detail(fundProjectInfo));
	}

	/**
	 * 分页
	 */
	@PostMapping("/list")
	@ApiOperation(value = "分页")
	public ListResult<FundProjectInfoVO> list(@RequestBody Query query) {
		List<SearchEntity> entityList = query.getSearch();
		query.setAscs("order_number");
		query.setDescs("create_time,id");

		QueryWrapper<FundProjectInfo> queryWrapper = SearchParamEntiy.getQueryWrapper(
				Condition.getQueryWrapper(new FundProjectInfo()), entityList
		);
		TokenSearch<FundProjectInfo> tokenSearch = new TokenSearch<>();
		tokenSearch.addTokenQuery(queryWrapper, query);

		if (query.getPageSize() < 0){
			queryWrapper.orderByAsc("order_number");
		}

		IPage<FundProjectInfo> pages = fundProjectInfoService.page(Condition.getPage(query), queryWrapper);
		ListResult<FundProjectInfoVO> listResult = new ListResult<>(CommonCode.SUCCESS);
		List<FundProjectInfoVO> fundProjectInfoVOS=fundProjectInfoWrapper.entityToVO(pages.getRecords());
		fundProjectInfoVOS.forEach(record->{
			Long id=record.getId();
			record.setFundProjectInfoId(id.toString());
		});
		listResult.setList(fundProjectInfoVOS);
		listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
		return listResult;
	}
	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Resubmit(delaySeconds = 0)
	@ApiOperation(value = "新增或修改")
	@LogOperation("市属国企基金项目情况-提交")
	public R submit(@Valid @RequestBody FundProjectInfoVO fundProjectInfoVO) {
		String checkMessage = fundProjectInfoService.checkName(fundProjectInfoVO);
		if (StringUtil.isNotBlank(checkMessage)){
			return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
		}
		return R.status(fundProjectInfoService.submit(fundProjectInfoVO));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除")
	@LogOperation("市属国企基金项目情况-删除")
	public R remove(@RequestBody PageData pd) {
		List<String> ids = (List<String>)pd.get("ids");
		return fundProjectInfoService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
				? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
	}

	/**
	 * 模板文件下载
	 * @param response
	 * @param os
	 */
	@GetMapping("/exportTemplateExcel")
//	@PassToken
	@ApiOperation(value = "模板文件下载")
	public void exportTemplateExcel(HttpServletResponse response, OutputStream os){
		fundProjectInfoService.exportTemplateExcel(response, os);
	}

	/**
	 * 覆盖导入 根据填报时间 填报单位覆盖或新增
	 */
	@PostMapping("/excelCoverImport")
	@LogOperation("市属国企基金项目情况-导入")
	@ApiOperation(value = "覆盖导入 根据填报时间 填报单位覆盖或新增")
	public ObjResult excelCoverImport(@RequestParam("file") MultipartFile multipartFile) throws IOException {
		String progressId = TokenUtil.getTokenUserId() + "_"+System.currentTimeMillis();
		CacheUtil.put("progress","progress",progressId + "#half:hour",0);
		InputStream inputStream = multipartFile.getInputStream();
		fundProjectInfoService.importExcel(progressId, inputStream);
		ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
		PageData pd = new PageData();
		pd.put("progressId", progressId);
		objResult.setPageData(pd);
		return objResult;
	}

	/**
	 * 导入进度查看
	 */
	@GetMapping("/progress/{progressId}")
	@ApiOperation(value = "导入进度查看")
	public ObjResult progress(@PathVariable String progressId){
		Object data = CacheUtil.get("progress", "progress", progressId + "#half:hour");
		if(data != null && data instanceof String){
			if ("导入成功".equals(data)){
				ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
				PageData pd = new PageData();
				pd.put("data", 200);
				objResult.setPageData(pd);
				return objResult;
			}
			return new ObjResult(CustomCode.builder().message((String)data).code(1000).success(false).build());
		}
		ObjResult objResult = new ObjResult(CommonCode.SUCCESS);
		PageData pd = new PageData();
		pd.put("data", data);
		objResult.setPageData(pd);
		return objResult;
	}


}
